/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PredictDlg.java
 *
 * Created on 2010-9-27, 10:22:56
 */
package dataanalysis.gui.dialog;

import dataanalysis.main.MainView;
import dataanalysis.Model;
import java.awt.Component;
import java.util.Vector;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

/**
 *
 * @author Administrator
 */
public class PredictDlg extends javax.swing.JDialog {

    private final MainView m_manager;
    private final Vector<String> m_strVector = new Vector();
    private final Vector<Model> m_modelVector = new Vector();
    private final Vector<Boolean> m_bFlags = new Vector();
    private int m_selectedIndex = -1;

    /** Creates new form PredictDlg */
    public PredictDlg(java.awt.Frame parent, boolean modal, MainView manager) {
        super(parent, modal);
        m_manager = manager;
        initComponents();
        m_list.setCellRenderer(new ListCellRenderer());
        this.setTitle("预测模型");
        this.setLocationRelativeTo(parent);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_list = new javax.swing.JList();
        m_addButton = new javax.swing.JButton();
        m_deleteButton = new javax.swing.JButton();
        m_cancleButton = new javax.swing.JButton();
        m_OKButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("选择预测模型:");

        m_list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        m_list.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                m_listValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(m_list);

        m_addButton.setText("添 加");
        m_addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_addButtonActionPerformed(evt);
            }
        });

        m_deleteButton.setText("删 除");
        m_deleteButton.setEnabled(false);
        m_deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_deleteButtonActionPerformed(evt);
            }
        });

        m_cancleButton.setText("取 消");
        m_cancleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_cancleButtonActionPerformed(evt);
            }
        });

        m_OKButton.setText("确 定");
        m_OKButton.setEnabled(false);
        m_OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_OKButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(m_OKButton)
                    .addComponent(m_deleteButton)
                    .addComponent(m_addButton)
                    .addComponent(m_cancleButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(m_addButton)
                        .addGap(18, 18, 18)
                        .addComponent(m_deleteButton)
                        .addGap(18, 18, 18)
                        .addComponent(m_cancleButton)
                        .addGap(18, 18, 18)
                        .addComponent(m_OKButton)
                        .addGap(60, 60, 60))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_addButtonActionPerformed
        m_manager.addModelAction();
    }//GEN-LAST:event_m_addButtonActionPerformed

    private void m_cancleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_cancleButtonActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_m_cancleButtonActionPerformed

    private void m_OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_OKButtonActionPerformed
        this.setVisible(false);
        m_manager.runDataPredictThread(m_modelVector.get(m_selectedIndex));
    }//GEN-LAST:event_m_OKButtonActionPerformed

    private void m_deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_deleteButtonActionPerformed
        if (m_selectedIndex != -1) {
            m_strVector.remove(m_selectedIndex);
            m_modelVector.remove(m_selectedIndex);
            this.updateList();
        }
    }//GEN-LAST:event_m_deleteButtonActionPerformed

    private void m_listValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_m_listValueChanged
        m_selectedIndex = m_list.getSelectedIndex();
        if (m_selectedIndex == -1) {
            m_deleteButton.setEnabled(false);
            m_OKButton.setEnabled(false);
            return;
        }
        m_deleteButton.setEnabled(true);
        m_OKButton.setEnabled(true);
        if (m_bFlags.get(m_selectedIndex).booleanValue() == false) {
            m_OKButton.setEnabled(false);
        }
    }//GEN-LAST:event_m_listValueChanged
//
//    /**
//    * @param args the command line arguments
//    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                PredictDlg dialog = new PredictDlg(new javax.swing.JFrame(), true);
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_OKButton;
    private javax.swing.JButton m_addButton;
    private javax.swing.JButton m_cancleButton;
    private javax.swing.JButton m_deleteButton;
    private javax.swing.JList m_list;
    // End of variables declaration//GEN-END:variables

    public void addModel(Model model, String path, boolean bEnabled) {
        if (model == null) {
            return;
        }
        String str = model.getType() + " @ " + path;
        m_strVector.add(str);
        m_modelVector.add(model);
        m_bFlags.add(bEnabled);
        this.updateList();
    }

    private void updateList() {
        m_list.setListData(m_strVector);
        m_list.updateUI();
    }

    public void updateState() {
        int size = m_modelVector.size();
        if (size == 0) {
            return;
        }
        for (int i = 0; i < m_modelVector.size(); i++) {
            Boolean b = m_manager.checkModelCapabilities(m_modelVector.get(i));
            m_bFlags.set(i, b);
        }
        //m_list.setCellRenderer(new ListCellRenderer());
        m_list.updateUI();
        int index = m_list.getSelectedIndex();
        if (index != -1) {
            m_OKButton.setEnabled(m_bFlags.get(m_list.getSelectedIndex()));
        }

    }

    class ListCellRenderer extends DefaultListCellRenderer {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (m_bFlags.get(index).booleanValue() == false) {
                setEnabled(false);
                setOpaque(true);
            }
            return this;
        }
    }
}
